package com.higgins.client.projectmanagement;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TabPanel;

public class ProjectManagementMobile implements EntryPoint {
	private final ProjectServiceAsync projectService = GWT
												.create(ProjectService.class);
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void onModuleLoad() {
		authenticate();
	}
	private void executeProjectManagement()	{
		final String level = Window.Location.getParameter("level");
		TabPanel tabPanel = new TabPanel();
		ProjectListBox listBox = new ProjectListBox(level);

		ProjectTable table = new ProjectTable();
		listBox.addListListener(table);
		DockLayoutPanel tasksPanel = new DockLayoutPanel(Unit.EM);
		tasksPanel.addNorth(listBox, 2);
		tasksPanel.add(table);
		final HistoryTab historyTab = new HistoryTab();
		final ImmediateTab immediateTab = new ImmediateTab();
		final FollowUpTab followUpTab = new FollowUpTab();
		
		tabPanel.add(tasksPanel, "Tasks");
		tabPanel.add(historyTab, "History");
		tabPanel.add(immediateTab, "Immediate");
		tabPanel.add(followUpTab, "FollowUp");
		tabPanel.selectTab(0);
		
		RootLayoutPanel.get().add(tabPanel);
		tabPanel.setSize("200em","100em");
		tasksPanel.setSize("200em","100em");
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>()    {

			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				int selection = event.getSelectedItem();
				
				switch (selection)	{
				case 0:
					break;
				case 1:
					historyTab.update();
					break;
				case 2:
					immediateTab.update(level);
					break;
				case 3:
					followUpTab.update();
					break;
				}
			}
		});
					
	}
	
	private void authenticate()	{
		projectService.checkAccess(new AsyncCallback<Boolean>()	{
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Boolean result) {
				executeProjectManagement();
			}
		});
	}
}
